package com.zff.rental.service.room;


import com.zff.rental.entity.statistics.RoomBrowsingHistoryEntity;
import com.zff.rental.entity.statistics.RoomClickContactRecordEntity;
import com.zff.rental.service.BaseService;
import com.zff.rental.vo.apartment.statistics.BrowsingRoomEndReq;
import com.zff.rental.vo.apartment.statistics.BrowsingRoomStartReq;
import com.zff.rental.vo.apartment.statistics.ClickContactRecordReq;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author HCY
 */
@Service
public class RoomStatisticsServer extends BaseService {
    /**
     * 用户开始浏览房间
     */
    public Long startBrowsingRoom(BrowsingRoomStartReq params) {
        RoomBrowsingHistoryEntity entity = new RoomBrowsingHistoryEntity();
        entity.setRoomId(params.getRoomId());
        entity.setUid(params.getUid());
        entity.setBrowsingStartTime(LocalDateTime.now());
        RoomBrowsingHistoryEntity saveResult = roomBrowsingHistoryRepository.save(entity);
        return saveResult.getId();
    }

    /**
     * 用户结束浏览房间
     */
    public void endBrowsingRoom(BrowsingRoomEndReq params) {
        RoomBrowsingHistoryEntity findResult = roomBrowsingHistoryRepository.findById(params.getId())
                .orElseGet(() -> {
                    RoomBrowsingHistoryEntity entity = new RoomBrowsingHistoryEntity();
                    entity.setRoomId(params.getRoomId());
                    entity.setUid(params.getUid());
                    return entity;
                });
        findResult.setBrowsingEndTime(LocalDateTime.now());
        roomBrowsingHistoryRepository.save(findResult);
    }

    /**
     * 点击联系记录
     */
    public void clickContactRecord(ClickContactRecordReq params) {
        RoomClickContactRecordEntity entity = new RoomClickContactRecordEntity();
        entity.setRoomId(params.getRoomId());
        entity.setUid(params.getUid());
        entity.setClickTime(LocalDateTime.now());
        entity.setWay(params.getWay());
        entity.setInfo(params.getInfo());
        roomClickContactRecordRepository.save(entity);
    }
}
